<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <link rel="stylesheet" href="../../css/print.css"/>

    <link href="../../legacy/plugins/bootstrap3.3.4/bootstrap.min.css" rel="stylesheet">
    <link href="../../legacy/css/bootstrap-overwrite.css" rel="stylesheet">
    <link href="../../legacy/plugins/jqpagination/jqpagination.css" rel="stylesheet">
    <link href="../../legacy/plugins/jqGrid/ui.jqgrid.css" rel="stylesheet">
    <link href="../../legacy/plugins/iCheck/custom.css" rel="stylesheet">
    <!--<link href="../../legacy/css/style.css" rel="stylesheet">-->
    <link href="../../legacy/css/mandy-style.css" rel="stylesheet">
    <link href="../../legacy/css/printer-gague.css" rel="stylesheet">
    <style>
        .tab-item-title {
            font-size: 12px;
        }

        .layui-tab-title > li {
            display: inline-flex;
            flex-direction: column;
            line-height: 1;
            height: 40px;
            justify-content: center;
            background-color: #fff;
            border-radius: 6px 6px 0 0;
        }

        .layui-tab-title > li::after {
            position: absolute;
            left: 0;
            top: 0;
            content: '';
            width: 100%;
            height: 40px;
            border: 1px solid #ccc;
            border-radius: 5px 5px 0 0;
            box-sizing: border-box;
        }

        .layui-tab-title > li.layui-this {
            color: #fff;
            background-color: #1AA094;
        }

        .layui-tab-title > li.layui-this::after {
            height: 40px;
            border-color: #1AA094;
            border-radius: 5px 5px 0 0;
        }
    </style>
</head>
<body class="ares-p print-wrap">
<button id="print-btn" class="layui-hide layui-btn layui-btn-xs ares-d-print-none ares-position-absolute ares-absolute-tr-offset">打印</button>
<label>基本信息</label>
<div class="ares-mb emr-info ares-border-bottom"></div>
<div class="ares-clearfix">
    <b id="gauge-name"></b>
    <div id="assessor" class="ares-float-right"></div>
</div>
<div class="layui-tab ares-d-print-none" lay-filter="tabs">
    <ul id="gauge-tabs-wrap" class="layui-tab-title"></ul>
</div>

<div id="gauge_content"></div>


<script src="../../plugins/jquery-3.2.1.min.js"></script>
<script src="../../legacy/plugins/jquery.cookie.js"></script>
<script src="../../legacy/plugins/jqGrid/i18n/grid.locale-en.js"></script>
<script src="../../legacy/plugins/jqGrid/jquery.jqGrid.min.js"></script>
<script src="../../legacy/plugins/jqpagination/jquery.jqpagination.min.js"></script>
<script src="../../legacy/plugins/toastr/toastr.min.js"></script>
<script src="../../legacy/plugins/iCheck/icheck.min.js"></script>
<script src="../../legacy/plugins/autosize.js"></script>
<script src="../../legacy/plugins/jquery.PrintArea.js"></script>
<script src="../../legacy/js/ivycloud.js"></script>
<script src="../../legacy/js/fast-recovery.js"></script>
<script src="../../legacy/js/reuse.js"></script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    var init, patientBirthday, mandyServices, utils, dayjs, form;
    var routerQuery, routerParams, gaugeRecordId, prcpItemId, type, editable;
    var isTherapist = false;
    var cacheData;

    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                return false;
            }
        }
        return true;
    }

    function submitGaugeResult() {
        var res = getGaugeResult();
        //如果校验出错直接返回
        if (!res) return false;
        console.log(res)
        var result = JSON.stringify(res);

        if (type == 're_evaluation') {
            //重新评估,新增项目且添加评估
            mandyServices.mandy_post_gauge_record_re_evaluation(prcpItemId, result).then(function (res) {
                utils.confirm(res.message + ',是否继续添加评估?', {icon: 3, title: '评估成功'}, function (index) {
                    init();
                    utils.layerClose(index);
                }, function (index) {
                    utils.layerClose(index);
                    if (isTherapist) {
                        top.history.back();
                    } else {
                        utils.closeCurrentSysTab();
                    }
                });
            });
        } else {
            mandyServices.mandy_post_gauge_record_result(gaugeRecordId, result).then(function (res) {
                utils.confirm(res.message + ',是否继续添加评估?', {icon: 3, title: '评估成功'}, function (index) {
                    init();
                    utils.layerClose(index);
                }, function (index) {
                    utils.layerClose(index);
                    if (isTherapist) {
                        top.history.back();
                    } else {
                        utils.closeCurrentSysTab();
                    }
                });
            });
        }
        return false;
    }

    function myReset() {
        $("#gauge_content form")[0].reset();
        getCount();
    }

    function fastEval() {
        var fastEvalValue = $("#select_fast_eval").val();
        if (fastEvalValue == 1) {
            var minRadios = $(".min");
            $.each(minRadios, function (idx, minRadio) {
                minRadio.checked = true;
            });
        } else if (fastEvalValue == 2) {
            var maxRadios = $(".max");
            $.each(maxRadios, function (idx, maxRadio) {
                maxRadio.checked = true;
            });
        }

        changeSelect();
    }

    layui.use(['form', 'element', 'utils', 'dayjs', 'greyServices', 'mandyServices', 'widget', 'qs', 'printArea'], function (_form, element, _utils, _dayjs, greyServices, _mandyServices, widget, qs, printArea) {
        mandyServices = _mandyServices;
        utils = _utils;
        form = _form;
        dayjs = _dayjs;
        isTherapist = utils.isTherapist();
        if (isTherapist) {
            top.layer = layui.layer;
        }else {
            $('#print-btn').removeClass('layui-hide');
        }
        init = function () {
            var localHash = location.hash,
                queryStr = isTherapist ? location.search.split('?')[1] : localHash.split('?')[1];
            routerQuery = queryStr ? qs.parse(queryStr) : {};
            routerParams = isTherapist ? routerQuery : top.layui.routers.params;
            if (!queryStr) return;
            console.log(localHash, queryStr, routerParams, routerQuery)
            gaugeRecordId = routerQuery.gaugeRecordId;
            prcpItemId = routerQuery.prcpItemId;
            type = routerQuery.type;
            editable = routerQuery.editable;
            getPrescriptionItem();
            getGaugeRecordByPrcpItemId(prcpItemId)
        }
        init();
        /**
         * @author: ares
         * @date: 2021/3/30 11:51
         * @description: 打印
         */
        $('#print-btn').on('click', function () {
            $('.print-wrap').printArea();
        })

        element.on('tab(tabs)', function (data) {
            var $this = $(this),
                currentPrcpItemId = $this.attr('lay-id');
            $('#gauge-name').text('评估项目: ' + cacheData.gauge_record_list[0].gauge.name);
            if (currentPrcpItemId == 'create') {
                type = 're_evaluation'; //借用重新评估的接口来新增
                loadEmptyGauge();
            } else {
                type = routerQuery.type; //还原默认类型
                loadHistoryGauge(currentPrcpItemId);
            }
        });

        /**
         * @author: ares
         * @date: 2021/3/31 10:27
         * @description: 查询处方项目,好像只是用来显示患者信息
         */
        function getPrescriptionItem() {
            mandyServices.mandy_get_prescription_item(prcpItemId).then(function (res) {
                var data = res.data,
                    medicalRecord = data.prescription.medical_record;
                patientBirthday = medicalRecord.patient.birthday;
                widget.createPatientBaseInfo($('.emr-info'), medicalRecord);
            });
        }

        function getGaugeRecordByPrcpItemId(prcpItemId) {
            mandyServices.mandy_get_gauge_record_by_prcp_item_id(prcpItemId).then(function (res) {
                var data = res.data || {},
                    gaugeRecordList = data.gauge_record_list || [];
                cacheData = data;
                renderTabs(gaugeRecordList);
                if (type == 're_evaluation') {
                    element.tabChange('tabs', 'create');
                } else {
                    element.tabChange('tabs', prcpItemId);
                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/31 11:08
         * @description: 渲染tabs
         * @param {array}list
         */
        function renderTabs(list) {
            if (!Array.isArray(list)) return;
            var $gaugeTabsWrap = $('#gauge-tabs-wrap'),
                $fragment = $(document.createDocumentFragment());
            $.each(list, function (idx, item) {
                var createTime = item.create_time.split(' '),
                    $tab = $('<li lay-id="' + item.prescription_item.id + '" class="ares-text-center">' +
                        '<div class="tab-item-title">' + createTime[0] + '</div>' +
                        '<div class="tab-item-title">' + createTime[1] + '</div>' +
                        '</li>');
                $fragment.append($tab);
            })
            $fragment.append('<li lay-id="create">新增</li>');
            $gaugeTabsWrap.empty().append($fragment);
        }

        /**
         * @author: ares
         * @date: 2021/3/31 11:31
         * @description: 渲染评定者
         * @param {object}data
         */
        function renderAssessor(data) {
            var $assessor = $('#assessor');
            if (data.status == 1) {
                $assessor.empty().append('<b>评定人: ' + data.medical.name + '</b>' +
                    '<b class="ares-ml-lg">评定时间: ' + dayjs(data.finish_time).format("YYYY-MM-DD HH:mm:ss") + '</b>');
            } else {
                $assessor.empty();
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/31 11:58
         * @description: 渲染评估表
         * @param {object}data
         */
        function renderGauge(data) {
            if (data.status) {
                if (IsPC()) {
                    $("#print-btn").removeClass("hide");
                }
                setEditable(false);
                setGaugeResult(JSON.parse(data.result));
            } else {
                setEditable(true);
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/31 11:59
         * @description: 加载历史评估表
         * @param {string}prcpItemId
         */
        function loadHistoryGauge(prcpItemId) {
            mandyServices.mandy_get_gauge_record('', prcpItemId).then(function (res) {
                var data = res.data;
                $("#gauge_content").load('/pages' + data.gauge.url, function () {
                    gaugeRecordId = data.id;
                    renderAssessor(data);
                    renderGauge(data);
                });
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/31 11:59
         * @description: 加载空白评估表
         */
        function loadEmptyGauge() {
            $("#gauge_content").load('/pages' + cacheData.gauge_record_list[0].gauge.url, function () {
                renderAssessor({});
                renderGauge({});
            });
        }
    })
</script>
</body>
</html>